"""
python 爬取外包平台数据

模块：
    requests >>> pip install requests
    scv
"""
from pprint import pprint

import requests
import csv
import os

# 如果文件不存在，则写入表头，如果存在，则不再写入表头
if not os.path.exists('外包数据.csv'):
    # 新建csv数据
    f = open('外包数据.csv', mode='a', encoding='utf-8', newline='')
    # 写入表头
    csv_write = csv.DictWriter(f, fieldnames=[
        '标题',
        '编号',
        '类型',
        '投标人数',
        '发布日期',
        '截止日期',
        '价格',
        '状态',
        '详情页'
    ])
    csv_write.writeheader()  # 写入表头
else:
    f = open('外包数据.csv', mode='a', encoding='utf-8', newline='')


for page in range(1, 11):
    url = 'https://www.jfh.com/jfportal/workMarket/getRequestData'

    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                      "Chrome/54.0.2840.99 Safari/537.36 "
    }

    data = {
        'putTime': '',
        'minPrice': '',
        'maxPrice': '',
        'isRemoteWork': '',
        'orderCondition': '0',
        'orderConfig': '1',
        'pageNo': page,
        'searchName': 'python',
        'fitCode': '0',
        'workStyleCode': '',
        'jfId': '235075506',
        'buId': '',
        'currentBuid': '',
        'jieBaoType': '',
        'login': '1',
        'city': '',
        'orderType': '',
        'serviceTypeKey': '',
        'webSite': '',
        'webSign': '',
        'source': '1'
    }

    response = requests.post(url=url, data=data, headers=headers)
    resultList = response.json()['resultList']
    for index in resultList:
        # pprint(index)
        try:
            index_url = f'https://www.jfh.com/jfportal/orders/jf{index["orderNo"]}'
            dict = {
                '标题': index['orderName'],
                '编号': index['orderNo'],
                '类型': index['techDirection'],
                '投标人数': index['bookCount'],
                '发布日期': index['formatOrderTime'],
                '截止日期': index['bidValidtime'],
                '价格': index['price'],
                '状态': index['bidValidTimeOut'],
                '详情页': index_url
            }
            csv_write.writerow(dict)
            print(dict)
        except Exception as e:
            pass
